package cn.zyjblogs.server.user.controller;

import cn.zyjblogs.server.user.dto.UserLoginDto;
import cn.zyjblogs.server.user.service.LoginService;
import cn.zyjblogs.server.user.vo.OAuth2AccessTokenVo;
import cn.zyjblogs.starter.common.entity.response.ResponseObject;
import cn.zyjblogs.starter.common.entity.response.ResponseResult;
import cn.zyjblogs.starter.web.apiversion.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhuyijun
 */
@Api(tags = {"登录认证管理"})
@RestController
@RequiredArgsConstructor
@Log4j2
@ApiVersion(1)
@RequestMapping("/{v}/user")
public class LoginController {
    private final LoginService loginService;

    @ApiOperation(value = "用户登录", notes = "用户登录")
    @ApiVersion(1)
    @PostMapping("/login")
    public ResponseObject<OAuth2AccessTokenVo> login(@RequestBody @Validated UserLoginDto userLoginDto) {
        return ResponseResult.success(loginService.login(userLoginDto));
    }

    @ApiOperation(value = "用户注销", notes = "用户注销")
    @ApiVersion(1)
    @PostMapping("/logout")
    public void logout() {
        loginService.logout();
    }

    @ApiOperation(value = "获取验证码", notes = "用户注销")
    @ApiVersion(1)
    @GetMapping("/getLoginCode")
    public ResponseObject sendLoginCode(@RequestParam("phone") String phone) {
        loginService.sendLoginCode(phone);
        return ResponseResult.success("发送成功");
    }
}
